package cn.b.sky.schedule.controller;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.common.model.Page;
import cn.b.sky.schedule.model.TimeJobInfoCondition;
import cn.b.sky.schedule.model.TimedJobInfo;
import cn.b.sky.schedule.service.TimedJobService;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.user.service.TimeZoneService;
import cn.b.sky.user.service.model.TimeZone;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <strong>Title : TimeJobController.java<br></strong>
 * <strong>Package : cn.b.sky.user.controller<br></strong>
 * <strong>Description : </strong>定时任务执行<br> 
 * <strong>Create on : 2015-9-14 上午11:18:24<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/timejob/*")
public class TimeJobController extends SecurityController {
  @Resource
  private TimedJobService timedJobService;

  @Resource
  private TimeZoneService timeZoneService;

  @RequestMapping("timeJobList")
  public String timeJobList(Model model, @ModelAttribute("condition") TimeJobInfoCondition condition, String navTabId) {
    condition.setSiteCode(this.getSiteCode());
    Page timejob = timedJobService.selectTimedJobList(condition);
    model.addAttribute("page", timejob);
    model.addAttribute(NAVTABID, navTabId);
    return "job/job_list";
  }

  @RequestMapping("addTimeJob")
  public String addTimeJob(Model model, String navTabId) {
    List<TimeZone> timeZone = timeZoneService.selectTimeZone();
    model.addAttribute("javaTimeZone", timeZone);
    model.addAttribute(NAVTABID, navTabId);
    return "job/job_add";
  }

  @RequestMapping("editTimeJob")
  public String editTimeJob(Model model, String navTabId, String jobCode) {
    if (StringUtils.isBlank(jobCode)) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    TimedJobInfo timedJobInfo = timedJobService.selectTimedJob(jobCode);
    List<TimeZone> timeZone = timeZoneService.selectTimeZone();
    model.addAttribute("javaTimeZone", timeZone);
    model.addAttribute("timeJob", timedJobInfo);
    model.addAttribute(NAVTABID, navTabId);
    return "job/job_add";
  }

  @RequestMapping("saveOrUpdateJob")
  @ResponseBody
  public DWZResult saveOrUpdateJob(Model model, String navTabId, TimedJobInfo timedJobInfo, String jobCodeId,
                                   HttpServletRequest request) {
    if (timedJobInfo == null) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    // 调整SiteCode赋值顺序，将其放置在业务操作之前，保证后面有关作业的配置信息是完整的
    timedJobInfo.setSiteCode(this.getSiteCode());

    if (!StringUtils.isBlank(jobCodeId)) {
      timedJobService.updateTimedConfig(timedJobInfo);
    } else {
      if (timedJobService.checkTimedJob(timedJobInfo.getJobCode())) {
        return ajaxError(this.getImeViewMessageI18n(getCurrentLanguage(), "existedTimeJob.M"));
      }
      timedJobService.saveTimedConfig(timedJobInfo);
    }
    //修改不关闭页面，也不刷新父页面
    String forwardUrl = getForwardUrl(request) + "/timejob/timeJobList?navTabId=" + navTabId;
    return dialogAjaxDone(navTabId, null, forwardUrl, null);
  }

  @RequestMapping("chooseExpression")
  public String chooseExpression(Model model, String cronExpression) {
    if (!StringUtils.isBlank(cronExpression)) {
      model.addAttribute("cronExpression", cronExpression);
    }
    return "job/job_expression";
  }

  @RequestMapping("doExecuteTimedJob")
  @ResponseBody
  public DWZResult doExecuteTimedJob(Model model, String jobCode) throws Exception {
    if (StringUtils.isBlank(jobCode)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    TimedJobInfo timedJobInfo = timedJobService.selectTimedJob(jobCode);
    timedJobService.doExecuteTimedJob(timedJobInfo);
    return ajaxDone(null);
  }

  @RequestMapping("deleteTimeJob")
  @ResponseBody
  public DWZResult removeTimedJob(Model model, String jobCode) {
    if (StringUtils.isBlank(jobCode)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    timedJobService.deleteTimedJob(jobCode);
    return ajaxDone(null);
  }
}
